package cn.dengta.webapp.user.vo.task;

import java.util.Date;

import cn.dengta.common.util.Formats;
import cn.dengta.context.model.WesKit;
import cn.dengta.webapp.base.entity.I18nContent;
import cn.dengta.webapp.base.vo.AbstractVO;
import cn.dengta.webapp.user.entity.task.TaskRule;
import cn.dengta.webapp.user.model.TaskType;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.X;

import static cn.dengta.webapp.user.entity.task.TaskRule.TaskRuleType;

@Getter
@Setter
public class TaskRuleVO extends AbstractVO<TaskRule> {

	/** 名称 */
	private Object name;
	/** 图标 */
	private String icon;
	/** 任务类型 */
	private TaskType type;
	/** 业务类型 */
	private TaskRuleType ruleType;
	/** 最小钻石 */
	private Integer minCoin;
	/** 最大钻石（范围值时候设置，否则为null） */
	private Integer maxCoin;
	/** 领取最大次数 */
	private Integer maxLimit;
	protected String remark;
	protected Boolean completed;
	protected Integer completeCount;
	protected String url;
	/** 排序 */
	private Integer sort;
	/** 开始时间 */
	private transient Date beginTime;
	/** 结束时间 */
	private transient Date endTime;
	/** 规则JSON配置 */
	private Object config;
	/** 概率规则JSON配置 */
	private Object factorConfig;
	private String wesKits;

	public static TaskRuleVO from(TaskRule source) {
		TaskRuleVO vo = copy(source, TaskRuleVO::new);
		if (vo != null) {
			vo.name = X.map(source.getResolvedLanguageCfg(), I18nContent::getDataMap);
			vo.config = source.getResolvedCfg();
			vo.factorConfig = source.getResolvedFactorCfg();
		}
		return vo;
	}

	public static TaskRuleVO fromSign(TaskRule source) {
		TaskRuleVO vo = copy(source, TaskRuleVO::new, "minCoin", "factorConfig", "sort", "remark");
		if (vo != null) {
			vo.config = source.getResolvedCfg();
		}
		return vo;
	}

	public static TaskRuleVO fromView(TaskRule source) {
		TaskRuleVO vo = copy(source, TaskRuleVO::new, "config", "factorConfig", "sort", "remark");
		if (vo != null) {
			vo.name = X.map(source.getResolvedLanguageCfg(), I18nContent::get);
		}
		return vo;
	}

	public Integer getRuleType() {
		return X.map(ruleType, TaskRuleType::ordinal);
	}

	public String getRuleType_() {
		return X.map(ruleType, TaskRuleType::getLabel);
	}

	public String getType_() {
		return X.map(type, TaskType::getDesc);
	}

	public Integer getType() {
		return X.map(type, TaskType::ordinal);
	}

	public String getBeginTime_() {
		return Formats.formatDefDate(beginTime);
	}

	public String getEndTime_() {
		return Formats.formatDefDate(endTime);
	}

	public String getWesKits_() {
		return WesKit.getWesKits_(wesKits);
	}

}
